import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "login" */ '../views/login/login.vue')
  },
  {
    path: '/',
    name: 'login',
    component: () => import(/* webpackChunkName: "login" */ '../views/login/login.vue')
  },
  {
    path: '/main',
    name: 'MainPage',
    beforeEnter: beforeEnterFun,
    component: () => import(/* webpackChunkName: "systemUser" */ '../views/pages/MainPage.vue')
  },
  {
    path: '/home',
    name: 'HomePage',
    beforeEnter: beforeEnterFun,
    component: () => import(/* webpackChunkName: "systemUser" */ '../views/pages/HomePage.vue')
  },
  {
    path: '/detail-information',
    name: 'DetailInformation',
    beforeEnter: beforeEnterFun,
    component: () => import(/* webpackChunkName: "systemUser" */ '../views/pages/DetailInformation.vue')
  },
  {
    path: '/energy-management',
    name: 'EnergyManagement',
    beforeEnter: beforeEnterFun,
    component: () => import(/* webpackChunkName: "systemUser" */ '../views/pages/EnergyManagement.vue')
  },
  {
    path: '/person-information',
    name: 'PersonInformation',
    beforeEnter: beforeEnterFun,
    component: () => import(/* webpackChunkName: "systemUser" */ '../views/pages/PersonInformation.vue')
  },
  {
    path: '/smart-building',
    name: 'SmartBuilding',
    beforeEnter: beforeEnterFun,
    component: () => import(/* webpackChunkName: "systemUser" */ '../views/pages/SmartBuilding.vue')
  },
  {
    path: '/smart-logistics',
    name: 'SmartLogistics',
    beforeEnter: beforeEnterFun,
    component: () => import(/* webpackChunkName: "systemUser" */ '../views/pages/SmartLogistics.vue')
  },
  {
    path: '/smart-catering',
    name: 'SmartCatering',
    beforeEnter: beforeEnterFun,
    component: () => import(/* webpackChunkName: "systemUser" */ '../views/pages/SmartCatering.vue')
  },
  {
    path: '/campus-planning',
    name: 'CampusPlanning',
    beforeEnter: beforeEnterFun,
    component: () => import(/* webpackChunkName: "systemUser" */ '../views/pages/CampusPlanning.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: '/chart/',
  routes
})

function beforeEnterFun (to, from, next) {
  if (!sessionStorage.getItem('token')) {
    router.push({ name: 'login' })
  } else {
    next()
  }
}

export default router
